package com.transaction.demo.notify.biz.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.transaction.demo.common.util.ResultPage;
import com.transaction.demo.notify.biz.NotifyRecordBiz;
import com.transaction.demo.notify.dao.NotifyRecordDao;
import com.transaction.demo.notify.entity.NotifyRecordBean;
import com.transaction.demo.notify.vo.req.NotifyRecordAddReqVo;
import com.transaction.demo.notify.vo.req.NotifyRecordListReqVo;
import com.transaction.demo.notify.vo.req.NotifyRecordReqVo;
import com.transaction.demo.notify.vo.req.NotifyRecordUpdateReqVo;
import com.transaction.demo.notify.vo.resp.NotifyRecordRespVo;

/**
 * 描述：通知记录表 RP_NOTIFY_RECORD
 * 作者：马庆智
 * 日期：2023/04/27 17:18:42
 */
@Service
public class NotifyRecordBizImpl implements NotifyRecordBiz {

	@Autowired
	private NotifyRecordDao notifyRecordDao;

	/**
	 * 查询通知记录表 RP_NOTIFY_RECORD
	 *
	 * @param notifyRecord 请求参数
	 * @return NotifyRecordResp
	 * @author 马庆智
	 * @date: 2023/04/27 17:18:42
	 */
	@Override
	public NotifyRecordRespVo queryNotifyRecord(NotifyRecordReqVo notifyRecord) {

		NotifyRecordBean query = notifyRecordDao.queryNotifyRecord(NotifyRecordBean.builder().build());

		return NotifyRecordRespVo.builder().build();
	}

	/**
	 * 查询通知记录表 RP_NOTIFY_RECORD列表
	 *
	 * @param notifyRecord 请求参数
	 * @return NotifyRecordResp
	 * @author 马庆智
	 * @date: 2023/04/27 17:18:42
	 */
	@Override
	public ResultPage<NotifyRecordRespVo> queryNotifyRecordList(NotifyRecordListReqVo notifyRecord) {

		PageHelper.startPage(notifyRecord.getCurrent(), notifyRecord.getPageSize());
		List<NotifyRecordBean> query = notifyRecordDao.queryNotifyRecordList(
				NotifyRecordBean.builder().merchantOrderNo(notifyRecord.getMerchantOrderNo()).build());
		PageInfo<NotifyRecordBean> pageInfo = new PageInfo<>(query);

		List<NotifyRecordRespVo> list = pageInfo.getList().stream().map(x -> {
			NotifyRecordRespVo resp = new NotifyRecordRespVo();
			BeanUtils.copyProperties(x, resp);
			return resp;
		}).collect(Collectors.toList());

		return new ResultPage<>(list, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());

	}

	/**
	 * 添加通知记录表 RP_NOTIFY_RECORD
	 *
	 * @param notifyRecord 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:18:42
	 */
	@Override
	public NotifyRecordRespVo saveNotifyRecord(NotifyRecordAddReqVo notifyRecord) {
		NotifyRecordBean recordBean = new NotifyRecordBean();
		BeanUtils.copyProperties(notifyRecord, recordBean);
		recordBean.setCreateTime(new Date());
		recordBean.setEditTime(new Date());

		notifyRecordDao.saveNotifyRecord(recordBean);

		NotifyRecordRespVo respVo = new NotifyRecordRespVo();
		BeanUtils.copyProperties(recordBean, respVo);
		return respVo;
	}

	/**
	 * 删除通知记录表 RP_NOTIFY_RECORD
	 *
	 * @param ids 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:18:42
	 */
	@Override
	public int deleteNotifyRecord(String ids) {
		return notifyRecordDao.deleteNotifyRecord(
				Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList()));
	}

	/**
	 * 更新通知记录表 RP_NOTIFY_RECORD
	 *
	 * @param notifyRecord 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:18:42
	 */
	@Override
	public int updateNotifyRecord(NotifyRecordUpdateReqVo notifyRecord) {
		NotifyRecordBean recordBean = new NotifyRecordBean();
		BeanUtils.copyProperties(notifyRecord, recordBean);
		return notifyRecordDao.updateNotifyRecord(recordBean);
	}

}